package com.nativetalk.service.three.live.comment;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtLiveComment;
import com.nativetalk.bean.comment.TdNtLiveCommentList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.TdNtLiveCommentDao;
import com.nativetalk.dao.TdNtLiveCourseHisDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/9/27.
 */
@Service
public class LiveCommentServiceImpl implements LiveCommentService {
    private static Logger logger = Logger.getLogger(LiveCommentServiceImpl.class);
    @Resource
    private TdNtLiveCommentDao tdNtLiveCommentDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Override
    @Transactional
    public synchronized RetInfo insertLiveComment(TdNtLiveComment tdNtLiveComment) {
        String logInfo = this.getClass().getName() + ":insertLiveComment:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtLiveComment.setCom_month(CommonUtil.getMonth(new Date()));
            tdNtLiveCommentDao.insertTdNtLiveComment(tdNtLiveComment);
            tdNtLiveCourseHisDao.updateLivesCommentSize(tdNtLiveComment.getLive_cou_his_id());
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("评论成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo list(Page page) {
        String logInfo = this.getClass().getName() + ":list:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveCommentDao.findTdNtLiveCommentByLiveHisIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveCommentList> tdNtLiveCommentLists = tdNtLiveCommentDao.findTdNtLiveCommentByLiveHisIdPage(page);
                if ( tdNtLiveCommentLists != null && tdNtLiveCommentLists.size() > 0) {
                    for (int i = 0 ; i < tdNtLiveCommentLists.size() ; i ++) {
                        TdNtLiveCommentList tdNtLiveCommentList = tdNtLiveCommentLists.get(i);
                        String name = CommonUtil.substring(tdNtLiveCommentList.getMemb_name(),0,1) + "****" +
                                CommonUtil.substring(tdNtLiveCommentList.getMemb_name(),tdNtLiveCommentList.getMemb_name().length() - 1,tdNtLiveCommentList.getMemb_name().length());
                        tdNtLiveCommentList.setMemb_name(name);
                    }
                }
                result.put("comments", tdNtLiveCommentLists);
                result.put("totalPage", totalPage);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播评论成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无直播评论");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
